//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BiasedBarrierPathPricer.h"
using namespace Cephei::QL::Pricingengines::Barrier;
#include <gen/QL/Methods/Montecarlo/Path.h>
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, QL::Option::TypeEnum type, Double strike, Cephei::Core::IVector<Double>^ discounts) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phBiasedBarrierPathPricer = NULL;
#endif
        QuantLib::Barrier::Type _barrierType = (QuantLib::Barrier::Type)barrierType ;
        QuantLib::Real _barrier = (QuantLib::Real)ValueHelper::Convert (barrier); //d
        QuantLib::Real _rebate = (QuantLib::Real)ValueHelper::Convert (rebate); //d
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        discounts->Lock();
        INativeVector<Double>^ _NCIdiscounts = discounts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdiscounts = safe_cast<CDoubleVector^>(_NCIdiscounts);
        std::vector<QuantLib::DiscountFactor>& _discounts = static_cast<std::vector<QuantLib::DiscountFactor>&> (_NCdiscounts->GetReference ());
        _ppBiasedBarrierPathPricer = new boost::shared_ptr<QuantLib::BiasedBarrierPathPricer> (new QuantLib::BiasedBarrierPathPricer ( _barrierType,  _barrier,  _rebate,  _type,  _strike,  _discounts ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (discounts != nullptr) discounts->Unlock();    //not optional
    }
}
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (boost::shared_ptr<QuantLib::BiasedBarrierPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiasedBarrierPathPricer = NULL;
#endif
	_ppBiasedBarrierPathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (QuantLib::BiasedBarrierPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBiasedBarrierPathPricer = NULL;
#endif
	_ppBiasedBarrierPathPricer = new boost::shared_ptr<QuantLib::BiasedBarrierPathPricer> (&childNative);
    
    _BiasedBarrierPathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (CBiasedBarrierPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiasedBarrierPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBiasedBarrierPathPricer = new boost::shared_ptr<QuantLib::BiasedBarrierPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiasedBarrierPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CBiasedBarrierPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (QuantLib::Handle<QuantLib::BiasedBarrierPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBiasedBarrierPathPricer = &childNative;
	_ppBiasedBarrierPathPricer = &static_cast<boost::shared_ptr<QuantLib::BiasedBarrierPathPricer>>(childNative.currentLink());
    
    _BiasedBarrierPathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (QuantLib::Handle<QuantLib::BiasedBarrierPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBiasedBarrierPathPricer = &childNative;
	_ppBiasedBarrierPathPricer = &static_cast<boost::shared_ptr<QuantLib::BiasedBarrierPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::CBiasedBarrierPathPricer (QuantLib::BiasedBarrierPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBiasedBarrierPathPricer = NULL;
#endif
	_ppBiasedBarrierPathPricer = new boost::shared_ptr<QuantLib::BiasedBarrierPathPricer> (new QuantLib::BiasedBarrierPathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::~CBiasedBarrierPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBiasedBarrierPathPricer != NULL)
    {
	    delete _ppBiasedBarrierPathPricer;
        _ppBiasedBarrierPathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::!CBiasedBarrierPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBiasedBarrierPathPricer != NULL)
    {
	    delete _ppBiasedBarrierPathPricer;
    }
}
QuantLib::BiasedBarrierPathPricer& Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::GetReference ()
{
    if (_ppBiasedBarrierPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppBiasedBarrierPathPricer;
}
boost::shared_ptr<QuantLib::BiasedBarrierPathPricer>& Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::GetShared ()
{
    if (_ppBiasedBarrierPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppBiasedBarrierPathPricer;
}
QuantLib::BiasedBarrierPathPricer* Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::GetPointer ()
{
    if (_ppBiasedBarrierPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppBiasedBarrierPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BiasedBarrierPathPricer>& Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::GetHandle ()
{
	if (_phBiasedBarrierPathPricer == NULL)
	{
		_phBiasedBarrierPathPricer = new Handle<QuantLib::BiasedBarrierPathPricer> (*_ppBiasedBarrierPathPricer);
	}
	return *_phBiasedBarrierPathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer::HasNative () 
{
	return (_ppBiasedBarrierPathPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Barrier::IBiasedBarrierPathPricer^ Cephei::QL::Pricingengines::Barrier::CBiasedBarrierPathPricer_Factory::Create (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, QL::Option::TypeEnum type, Double strike, Cephei::Core::IVector<Double>^ discounts)
{
    return REFNEW CBiasedBarrierPathPricer ( barrierType,  barrier,  rebate,  type,  strike,  discounts);
}
